package com.bc.datasource;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.bc.datasource.vo.SendObject;
import com.bc.util.DoubleUtil;

import jxl.Workbook;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class ScanUtils {

	
	public static boolean cb(Date d,Date d1,int minutis){
		return d.getTime()<(d1.getTime()+minutis*60*1000);
	}
	
	
	public static boolean ca(Date d,Date d1,int minutis){
		return d.getTime()>(d1.getTime()+minutis*60*1000);
	}
	
	
	public static boolean cb(Date d,int minutis){
		return d.getTime()<(new Date().getTime()+minutis*60*1000);
	}
	
	
	public static boolean ca(Date d,int minutis){
		return d.getTime()>(new Date().getTime()+minutis*60*1000);
	}
	
	/**
     * A方法追加文件：使用RandomAccessFile
     */
    public static void appendMethodA(String fileName, String content) {
        try {
            RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
            long fileLength = randomFile.length();
            randomFile.seek(fileLength);
            randomFile.writeUTF(content);
            randomFile.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static InputStream loadConfig(String name) throws Exception{
		InputStream stream = null;
		name = (name.startsWith("/"))? name.substring(1) : name;
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		stream = loader.getResourceAsStream(name);
		if (stream == null){
			throw new Exception("load error");
		}
		return stream;
	}
    
    public static Properties loadPro(String name) throws Exception{
    	Properties property = new Properties();
		property.load(loadConfig(name));
		return property;
    }


    static SimpleDateFormat sd = new SimpleDateFormat("yyyyMMdd");
	/**
	 * @param dateS
	 * @return
	 * @throws ParseException
	 */
	public static Calendar getStartDate(String dateS) throws ParseException {
		Date date = sd.parse(dateS);
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		//c.add(Calendar.DATE, 1);
		return c;
	}
	/**
	 * @param dateS
	 * @return
	 * @throws ParseException
	 */
	public static Calendar getNextDate(Date date) throws ParseException {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DATE, 1);
		return c;
	}
	
	/**
	 * @param dateS
	 * @return
	 * @throws ParseException
	 */
	public static Calendar getPreDate(Date date) throws ParseException {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DATE, -1);
		return c;
	}
	
	
	/**
	 * @param ss
	 * @return
	 * @throws Exception 
	 */
	public static String getGameTime(String[] ss) throws Exception {
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dat = ss[36];
		String[] dd = dat.split("-");
		if(dd[0].length()<2)
			dd[0]= "0"+dd[0];
		if(dd[1].length()<2)
			dd[1]= "0"+dd[1];
		String td = ss[43]+"-"+dd[0]+"-"+dd[1]+" "+ss[11]+":00";
		Date d = sdf1.parse(td);
		return sdf1.format(d);
	}
	
	/**
	 * @param ss
	 * @return
	 * @throws Exception 
	 */
	public static Date getGameTimeDate(String[] ss) throws Exception {
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dat = ss[36];
		String[] dd = dat.split("-");
		if(dd[0].length()<2)
			dd[0]= "0"+dd[0];
		if(dd[1].length()<2)
			dd[1]= "0"+dd[1];
		String td = ss[43]+"-"+dd[0]+"-"+dd[1]+" "+ss[11]+":00";
		return sdf1.parse(td);
	}
	
	
	public static void makeTask(List<SendObject> games) throws Exception {
		int count = 0;
		for (Iterator<SendObject> iterator = games.iterator(); iterator.hasNext();) {
			count =count + sG(iterator);
		}
        System.out.println("共创建"+count+"个任务");
	}

	/**
	 * @param count
	 * @param iterator
	 * @return
	 * @throws ParseException
	 */
	private static int sG(Iterator<SendObject> iterator) throws ParseException {
		SendObject game = iterator.next();
		String gameTime = game.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date gameDate = sdf.parse(gameTime);
		long now = new Date().getTime();
		long gamelong = gameDate.getTime();
		long still = gamelong - now;
		//提前5分钟推送,赛前5分钟内得到比赛的 立刻推送
		if(still>=5*60*1000){
//			ScanTask scanTask = new ScanTask();
//		    Timer timer = new Timer();
//		    scanTask.setGame(game);
//		    timer.schedule(scanTask, still-5*60*1000);
		    return 1;
		}else if(5*60*1000>still&&still>0){
//			ScanTask scanTask = new ScanTask();
//		    Timer timer = new Timer();
//		    scanTask.setGame(game);
//		    timer.schedule(scanTask, 5);
		    return 1;
		}
		return 0;
	}
	
	public static double getPan(String pan){
		//平手 平手/半球 半球  半球/一球   一球/球半
		boolean isShou = false;
		double pank = 0.1d;
		if(pan.contains("受")){
			isShou=true;
			pan = pan.replace("受", "");
		}
		
		if("平手".equals(pan)){
			pank= 0d;
		}
		
		if("平手/半球".equals(pan)||"让平手/半球".equals(pan)){
			pank= 0.25d;
		}
		
		if("半球".equals(pan)||"让半球".equals(pan)){
			pank= 0.5d;
		}
		
		if("半球/一球".equals(pan)||"让半球/一球".equals(pan)){
			pank= 0.75d;
		}
		
		if("一球".equals(pan)||"让一球".equals(pan)){
			pank= 1d;
		}
		
		if("一球/球半".equals(pan)||"让一球/球半".equals(pan)){
			pank= 1.25d;
		}
		
		if("球半".equals(pan)||"让球半".equals(pan)){
			pank= 1.5d;
		}
		
		if("球半/两球".equals(pan)||"让球半/两球".equals(pan)){
			pank= 1.75d;
		}
		
		if("两球".equals(pan)||"让两球".equals(pan)){
			pank= 2d;
		}
		
		if("两球/两球半".equals(pan)||"让两球/两球半".equals(pan)){
			pank= 2.25d;
		}
		
		if("两球半".equals(pan)||"让两球半".equals(pan)){
			pank= 2.5d;
		}
		
		if("两球半/三球".equals(pan)||"让两球半/三球".equals(pan)){
			pank= 2.75d;
		}
		
		if("三球".equals(pan)||"让三球".equals(pan)){
			pank= 3d;
		}
		
		if("三球/三球半".equals(pan)||"让三球/三球半".equals(pan)){
			pank= 3.25d;
		}
		
		if("三球半".equals(pan)||"让三球半".equals(pan)){
			pank= 3.5d;
		}
		
		if("三球半/四球".equals(pan)||"让三球半/四球".equals(pan)){
			pank= 3.75d;
		}
		
		if("四球".equals(pan)||"让四球".equals(pan)){
			pank= 4d;
		}
		
		if("四球/四球半".equals(pan)||"让四球/四球半".equals(pan)){
			pank= 4.25d;
		}
		
		if("四球半".equals(pan)||"让四球半".equals(pan)){
			pank= 4.5d;
		}
		
		if(isShou){
			pank = pank - 2*pank;
		}
		
		return pank;
	}
	
	
	
	
	public static double getPanBig(String pan){
		//平手 平手/半球 半球  半球/一球   一球/球半
		boolean isShou = false;
		double pank = 0.1d;
		if("1".equals(pan)){
			pank= 1d;
		}
		
		if("1/1.5".equals(pan)){
			pank= 1.25d;
		}
		
		if("1.5".equals(pan)){
			pank= 1.5d;
		}
		
		if("1.5/2".equals(pan)){
			pank= 1.75d;
		}
		
		if("2".equals(pan)){
			pank= 2d;
		}
		
		if("2/2.5".equals(pan)){
			pank= 2.25d;
		}
		
		if("2.5".equals(pan)){
			pank= 2.5d;
		}
		
		if("2.5/3".equals(pan)){
			pank= 2.75d;
		}
		
		if("3".equals(pan)){
			pank= 3d;
		}
		
		if("3/3.5".equals(pan)){
			pank= 3.25d;
		}
		
		if("3.5".equals(pan)){
			pank= 3.5d;
		}
		
		if("3.5/4".equals(pan)){
			pank= 3.75d;
		}
		
		if("4".equals(pan)){
			pank= 4d;
		}
		
		if("4/4.5".equals(pan)){
			pank= 4.25d;
		}
		
		if("4.5".equals(pan)){
			pank= 4.5d;
		}
		
		return pank;
	}
	
	
	/**
	 * @param mails
	 * @return
	 */
	public static  String[] readMailConfig(String[] mails) {
		try {
			FileInputStream fi = new FileInputStream(new File("c://mail.txt"));
			byte[] data = new byte[fi.available()];
			fi.read(data);
			String mail = new String(data);
			mails = mail.split(";");
		} catch (FileNotFoundException e) {
			//System.out.println("邮件配置文件不存在");
		} catch (IOException e) {
			//System.out.println("邮件配置文件读取错误");
		}
		return mails;
	}
	

	
	public static void isRight(SendObject so) {
		if(so.getResult()!=null){
			String[] rs = so.getResult().split("-");
			String rr = "";
			double i1 = Double.valueOf(rs[0]);
			double i2= Double.valueOf(rs[1]);
			
			if(i1-ScanUtils.getPan(so.getPankou())-i2>0){
				rr = so.getTeamHome();
			}else if(i1-ScanUtils.getPan(so.getPankou())==i2){
				
			}else{
				rr = so.getTeamVisit();
			}
			
			if(rr.equals("")){
				so.setYanRight("走");
				so.setG("1");
				return;
			}
			
			if(rr.equals(so.getPro().replace("-", "").replace("+", ""))){
				so.setYanRight("赢");
				if(so.getPro().contains(so.getTeamHome())){
					so.setOddRight(so.getYaHome2());
				}else{
					so.setOddRight(so.getYaVisit2());
				}
				
				so.setG(""+DoubleUtil.sum(DoubleUtil.sub(so.getOddRight(), "0"),1d));
				if(i1-ScanUtils.getPan(so.getPankou())-i2==0.25d||i1-ScanUtils.getPan(so.getPankou())-i2==-0.25d){
					so.setYanRight("赢半");
					so.setG(""+DoubleUtil.sum(DoubleUtil.sub(so.getOddRight(), "0")/2,1d));
					//so.setG("1.5");
				}
			}else{
				so.setYanRight("输");
				so.setG("0");
				if(i1-ScanUtils.getPan(so.getPankou())-i2==0.25d||i1-ScanUtils.getPan(so.getPankou())-i2==-0.25d){
					so.setYanRight("输半");
					so.setG("0.5");
				}
			}
			
			
		}
		
	}

	public static boolean filtLeagal(SendObject so) {
		return false;
	}
	
	public static boolean filtLeagalBypan(SendObject so ){
		return false;
	}
	
	public static boolean filtLeagal() {

		return false;
	}
	
	
	public static Map<String, Integer> getLps(){
		Map<String,Integer> lps = new HashMap<String, Integer>();
		
		return lps;
	}
	
	
}
